'''
将vector平均到文件中
属于预操作
'''

import json
import os
import numpy

def average_symbol2file(vector,dic,root,dim):
    jsonfile=os.path.join(root,"raw","filedefsymbol.json")
    with open(jsonfile, 'r', encoding='utf8')as fp:
        returnvector=[]
        with open(os.path.join(root,"processed","idx.name.csv"),"w")as fp2:
            idx=0
            json_data = json.load(fp)
            for file in json_data:
                ve=[]
                for symbol in file['symbol']:
                    if dic.get(str(symbol)) is not None:
                        ve.append(vector[dic.get(str(symbol))].numpy())
                if len(ve)==0:
                    ve=numpy.zeros(dim)
                else:
                    ve = numpy.mean(ve, axis=0)
                fp2.writelines(str(idx)+" "+file['name']+"\n")
                idx+=1
                returnvector.append(ve)

    return returnvector




